﻿using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WP7Config
{
    /// <summary>
    /// Interaction logic for StartWindow.xaml
    /// </summary>
    public partial class StartWindow : Window
    {
        private string sDir;

        public StartWindow()
        {
            InitializeComponent();
        }

        private void btnConfigure_Click(object sender, RoutedEventArgs e)
        {
            wndConfig wndconfig = new wndConfig();
            wndconfig.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string sExec = Assembly.GetExecutingAssembly().Location;
            int i = sExec.LastIndexOf("\\");
            sDir = sExec.Substring(0, i) + "\\";

            ServiceController service = new ServiceController();
            service.ServiceName = "PrintService";
            try
            {
                ServiceControllerStatus status = service.Status;
                btnInstall.IsEnabled = false;
                btnConfigure.IsEnabled = true;
                btnRemove.IsEnabled = true;
            }
            catch
            {
                btnInstall.IsEnabled = true;
                btnConfigure.IsEnabled = false;
                btnRemove.IsEnabled = false;
            }

        }
        private void btn_MouseEnter(object sender, MouseEventArgs e)
        {
            Button btn = sender as Button;
            StackPanel stp = btn.Content as StackPanel;
            foreach (object oj in stp.Children)
            {
                TextBlock tbl = oj as TextBlock;
                if (tbl != null)
                    tbl.Foreground = new SolidColorBrush(Colors.IndianRed);
            }
        }

        private void btn_MouseLeave(object sender, MouseEventArgs e)
        {
            Button btn = sender as Button;
            btn.BorderThickness = new Thickness(0.0);
            StackPanel stp = btn.Content as StackPanel;
            foreach (object oj in stp.Children)
            {
                TextBlock tbl = oj as TextBlock;
                if (tbl != null)
                    tbl.Foreground = new SolidColorBrush(Colors.DarkBlue);
            }
        }

        private void btnInstall_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;
            if (MessageBox.Show("Installing the WP7 Print Helper service. This may take some time.\n" +
                                "Click on the OK button to continue, or on the Cancel button to stop installation",
                                "WP7 Print Helper Configuration",
                                MessageBoxButton.OKCancel, MessageBoxImage.Information) == MessageBoxResult.Cancel)
                return;

            bool bReturn = InstallService(sDir);
            this.Cursor = Cursors.Arrow;

            if (bReturn)
            {
                MessageBox.Show("The WP7 Print Helper service has installed successfully.\n" +
                                "You will have to configure the service before starting it.",
                                "WP7 Print Helper Configuration",
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                btnInstall.IsEnabled = false;
                btnConfigure.IsEnabled = true;
                btnRemove.IsEnabled = true;
            }
            else
            {
                MessageBox.Show("The WP7 Print Helper service has not installed successfully.\n" +
                                "Check the Windows Event Log for the error.",
                                "WP7 Print Helper Configuration",
                                MessageBoxButton.OK, MessageBoxImage.Stop);
                System.Windows.Application.Current.Shutdown();
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            bool bReturn;

            this.Cursor = Cursors.Wait;
            wndConfig wndconfig = new wndConfig();
            if (wndConfig.StopService("PrintService", 10000))
                bReturn = UninstallService(sDir);
            else
                bReturn = false;
            wndConfig.DeleteSettings();
            this.Cursor = Cursors.Arrow;

            if (bReturn)
            {
                MessageBox.Show("The WP7 Print Helper service has uninstalled successfully.",
                                "WP7 Print Helper Configuration",
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                btnInstall.IsEnabled = true;
                btnConfigure.IsEnabled = false;
                btnRemove.IsEnabled = false;
            }
            else
            {
                MessageBox.Show("The WP7 Print Helper service has not uninstalled successfully.\n" +
                                "Check the Windows Event Log for the error.",
                                "WP7 Print Helper Configuration",
                                MessageBoxButton.OK, MessageBoxImage.Stop);
                System.Windows.Application.Current.Shutdown();
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            System.Windows.Application.Current.Shutdown();
        }

        private bool InstallService(string sPath)
        {
            ServiceProcessInstaller ProcesServiceInstaller = new ServiceProcessInstaller();
            ProcesServiceInstaller.Account = ServiceAccount.LocalSystem;
            //ProcesServiceInstaller.Username = "<<username>>";
            //ProcesServiceInstaller.Password = "<<password>>";

            ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
            InstallContext Context = new System.Configuration.Install.InstallContext();
            String path = String.Format("/assemblypath={0}WP7PrintService.exe", sPath);
            String[] cmdline = { path };

            Context = new System.Configuration.Install.InstallContext("", cmdline);
            ServiceInstallerObj.Context = Context;
            ServiceInstallerObj.DisplayName = "PrintService";
            ServiceInstallerObj.Description = "PrintService installation";
            ServiceInstallerObj.ServiceName = "PrintService";
            ServiceInstallerObj.StartType = ServiceStartMode.Automatic;
            ServiceInstallerObj.Parent = ProcesServiceInstaller;

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            ServiceInstallerObj.Install(state);
            return true;
        }

        private bool UninstallService(string sPath)
        {
            ServiceInstaller ServiceInstallerObj = new ServiceInstaller(); 
            InstallContext Context = new InstallContext("", null); 
            ServiceInstallerObj.Context = Context;
            ServiceInstallerObj.ServiceName = "PrintService";
            try
            {
                ServiceInstallerObj.Uninstall(null);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
